package com.xuecheng.media.Java编程;


import java.util.*;

/**
 * 建议61：若有必要，使用变长数组
 */
public class 建议61 {
    public static void main(String[] args) {
        Stu[] stus = new Stu[60];
        expandCapacity(stus, 100);
    }


    public static <T> T[] expandCapacity(T[] datas, int newLen) {
        // 不能是负值
        newLen = newLen < 0 ? 0 : newLen;
        // 生成一个新数组，并拷贝原值
        return Arrays.copyOf(datas, newLen);
    }


    public static class Stu {
        private String name;
        private int age;
        private String sex;
    }
}

/**
 * aList产生的方法对象不可更改
 */
class 建议66 {
    public static void main(String[] args) {
        Week[] workDays = {Week.MONDAY, Week.TUESDAY, Week.WEDNESDAY, Week.THURSDAY, Week.FRIDAY, Week.SATURDAY};
        //转换为列表
        List<Week> list = Arrays.asList(workDays);
        //增加周日为工作日
        list.add(Week.SUNDAY);
        /* 工作日开始干活了*/
    }

    enum Week {
        MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
    }
}


/**
 * 建议67 不同的列表选择不同的遍历方法
 */
class 建议67 {
    public static void main(String[] args) {
        //学生数量80万
        int stuNum = 80 * 10000;
        List<Integer> stu = new ArrayList<>(stuNum);
        for (int i = 0; i < stuNum; i++) {
            stu.add(new Random().nextInt(150));
        }
        //记录开始计算时间
        long start = System.currentTimeMillis();
        System.out.println("平均分是" + average(stu));
        System.out.println("执行时间" + (System.currentTimeMillis() - start));
    }

    public static int average(List<Integer> stu) {
        int sum = 0;
        for (int i : stu) {
            sum += i;
        }
        //除以人数，计算平均值
        return sum / stu.size();
    }


    public static int average1(List<Integer> stu) {
        int sum = 0;
        for (int i = 0, size = stu.size(); i < size; i++) {
            sum +=stu.get(i);
        }
        //除以人数，计算平均值
        return sum / stu.size();
    }

}


/**
 * 频繁插入和删除时要使用LinkedList
 */
class  建议68 {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);

    }
 }